# Copyright 2011-2015 Free Software Foundation, Inc.
#
# Contributed by Red Hat, originally written by Keith Seitz.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is part of the gdb testsuite.

proc add {var name params expected {kind {func}}} {
    upvar $var result

    if {[string compare $kind "template"] == 0} {
	set method_name "${name}<$expected>"
    } else {
	set method_name "$name"
    }

    set expect ".*// ${method_name}\\($expected\\)"
    lappend result [list "${method_name}($params)" $expect]
}

if {[skip_cplus_tests]} { continue }

# Tests for c++/12266 et al
standard_testfile .cc

if {[prepare_for_testing $testfile $testfile $srcfile {c++ debug}]} {
    return -1
}

if {![runto_main]} {
    perror "couldn't run to breakpoint"
    continue
}

array set typedefs {
    "my_other_type" {"my_other_type" "my_type" "const char* const*"}
    "my_other_type_2" {"my_other_type_2" "my_type_2" "int"}
    "CORE_ADDR" { "CORE_ADDR" "unsigned long" }
    "_BAR_" { "_BAR_" "a::b::BAR" "a::b::c::d::bar" }
    "aenum" { "aenum" "anon_enum" }
    "astruct" { "astruct" "anon_struct" }
    "aunion" { "aunion" "anon_union" }
}

set methods {}

# Add the simple, one-parameter methods
foreach meth {A::FOO::test A::FOO::foo} {
    foreach type {my_other_type my_other_type_2} {
	foreach t $typedefs($type)  {
	    add methods $meth $t $type
	}
    }
}

# Add two-parameter methods
foreach meth {A::FOO::test A::FOO::foo} {
    set type "my_other_type_2, const my_other_type"
    foreach t1 $typedefs(my_other_type_2) {
	foreach t2 $typedefs(my_other_type) {
	    add methods $meth "$t1, const $t2" $type
	    add methods $meth "$t1, $t2" $type
	}
    }
}

# Add three-parameter methods/functions
foreach meth {A::FOO::test A::FOO::foo B::test test} {
    set type "aenum, astruct const&, aunion const\\*\\*\\*"
    foreach t1 $typedefs(aenum) {
	foreach t2 $typedefs(astruct) {
	    foreach t3 $typedefs(aunion) {
		add methods $meth "$t1, $t2 const&, $t3 const***" $type
	    }
	}
    }
}

# Add the array-of-function pointer methods
set type "fptr1\\*"
foreach meth {A::FOO::test A::FOO::foo} {
    add methods $meth "fptr1*" $type
    foreach t $typedefs(my_other_type) {
	add methods $meth "void (**) ($t)" $type
    }
}

# Add the function pointer methods
set type "fptr3"
foreach meth {A::FOO::test A::FOO::foo} {
    add methods $meth "fptr3" $type

    foreach t1 $typedefs(my_other_type) {
	add methods $meth "void (*)(fptr2, $t1)" $type
	foreach t2 $typedefs(my_other_type_2) {
	    add methods $meth "void (*)(void (*)(fptr1, $t2), $t1)" $type
	    foreach t3 $typedefs(my_other_type) {
		add methods $meth \
		    "void (*)(void (*)(void (*) ($t3), $t2), $t1)" $type
	    }
	}
    }
}

set type1 "my_other_type"
set type2 "my_other_type, my_other_type_2"
foreach meth {"test" "B::test"} {
    foreach t1 $typedefs(my_other_type) {
	add methods $meth $t1 $type1
	foreach t2 $typedefs(my_other_type_2) {
	    add methods $meth "$t1, $t2" $type2 template
	}
    }
}

# Miscellaneous tests
set type {CORE_ADDR \(\*\) \[10\]}
foreach meth {A::FOO::foo A::FOO::test} {
    foreach t $typedefs(CORE_ADDR) {
	add methods $meth "$t (*) \[10\]" $type
    }
}

foreach t $typedefs(_BAR_) {
    add methods "test" "$t&" {_BAR_&}
}

gdb_test_no_output "set listsize 1" ""

# Finally, for each method in the list METHODS, check whether
# the user can "list" it and "break" on it (both quoted and unquoted).
foreach test $methods {
    set func [lindex $test 0]
    set result [lindex $test 1]

    gdb_test "list $func" $result
    gdb_test "list '$func'" $result
    if {[gdb_breakpoint $func]} {
      pass "break $func"
    }
    if {[gdb_breakpoint '$func']} {
      pass "break '$func'"
    }
}

gdb_exit
return 0
